<template>
  <div class="hospitalsign">
    <div class="top">
      <div class="title">参加社会兼职申请</div>
      <div class="topinfo">
        <div class="post">兼职的社会组织名称</div>
        <div>
          <input
            type="text"
            placeholder="请输入"
            v-model="form.shzzmc"
          />
        </div>
      </div>
      <div class="topinfo">
        <div class="post">业务主管单位</div>
        <div>
          <input
            type="text"
            placeholder="请输入"
            v-model="form.ywzgdw"
          />
        </div>
      </div>
      <div class="topinfo">
        <div class="post">兼任职务</div>
        <div>
          <input
            type="text"
            placeholder="请输入"
            v-model="form.jzzw"
          />
        </div>
      </div>
      <div class="topinfo">
        <div class="post">兼职时间</div>
        <div :class="{'empty-style': !form.jzsj}" @click="showDatePicker('jzsj')">{{form.jzsj | formatDate}}</div>
      </div>
      <div class="topinfo">
        <div class="post">兼职数量</div>
        <div>
          <input
            type="number"
            placeholder="请输入"
            v-model="form.jzsl"
          />
        </div>
      </div>
      <div class="topinfo" style="border-bottom: none;">
        <div class="post">文件</div>
        <div>
          <van-uploader :preview-image="false" v-model="fileList" :after-read="afterRead">
            <van-button icon="plus" type="primary"><span class="ploadSies">上传文件</span></van-button>
          </van-uploader>
        </div>
      </div>
      <div class="topinfo noborder" v-show="fileList.length">
        <div v-for="(val,index) in fileList" :key="index">
          <span class="ploadSies">{{val.file.name}}</span><van-icon name="cross" @click="removeImage(val)" />
        </div>
        <div></div>
      </div>
    </div>
    <div class="sign-btn" @click="submit">提交</div>
    <div class="historylist">
      <p class="historytitle">历次申请记录</p>
      <div class="item" v-for="(item, i) in list" :key="i" @click="todetail(item.jzxxId)">
        <div>
          <p>申请日期：{{ item.createTime }}</p>
          <p>{{ item.shzzmc }}</p>
        </div>
        <div><van-icon name="arrow" /></div>
      </div>
    </div>
    <date-picker
      @fetchData="fetchDate"
      date-type="date"
      :default-date="form.jzsj"
      :show="isDatePickerShow"
      @cancel="hideDatePicker"
      @hide="hideDatePicker"
    ></date-picker>
    <van-dialog v-model="dialogShow" title="提示" show-cancel-button @confirm="save" @cancel='dialogShow=false'>
      请您按照《全国人大机关退（离）休干部社会兼职管理办法》的规定准备好相关材料后报局办公室
    </van-dialog>
  </div>
</template>

<script>
import datePicker from '../components/datePicker';
import moment from 'moment';
import {cjshjzsjaddCjshjzsj, cjshjzsjquerydata4list, cjshjzsjquerydata4object, ryjbxxQuerydata4object, uploadoneattachment} from '../assets/js/api';
import { Dialog } from 'vant';
export default {
  filters: {
    formatDate(v) {
      return (v && moment(v).format('YYYY-MM-DD')) || '请选择';
    }
  },
  components: {
    datePicker
  },
  data() {
    return {
      form: {
        shzzmc: '',
        ywzgdw: '',
        jzzw: '',
        jzsj: '',
        jzsl: '',
        images: []
      },
      isDatePickerShow: false,
      list: [],
      fileList: [],
      dialogShow: false,
      dspToken: localStorage.dspToken
    };
  },
  mounted() {
    ryjbxxQuerydata4object({'dspToken': this.dspToken}).then(({data}) => {
      const extendInfo = data[0];
      if (Number(extendInfo.age) >= 70) {
        Dialog.alert({
          message: '兼职的任职年龄界限为70周岁。'
        }).then(() => {
          history.go(-1);
        });
      }
    });
    this.gethistoryList();
  },
  methods: {
    gethistoryList() {
      cjshjzsjquerydata4list({
        dspToken: this.dspToken
      }).then(res => {
        this.list = res.data;
      });
    },
    todetail(v) {
      cjshjzsjquerydata4object({
        jzxxId: v,
        dspToken: this.dspToken
      }).then(res => {
        let x = res.data[0];
        this.$router.push({
          name: 'detail',
          query: {
            title: '社会兼职详情',
            data: JSON.stringify({
              '兼职的社会组织名称': x.shzzmc,
              '业务主管单位': x.ywzgdw,
              '兼任职务': x.jzzw,
              '兼职时间': x.jzsj,
              '兼职数量': x.jzsl
            })
          }
        });
      });
    },
    submit() {
      let keys = Object.keys(this.form), obj = this.form;
      for (let i = 0, l = keys.length; i < l; i++) {
        if (obj[keys[i]] === '') {
          this.$toast('请完善信息');
          return false;
        }
      }
      this.dialogShow = true;
    },
    save() {
      cjshjzsjaddCjshjzsj({
        shzzmc: this.form.shzzmc,
        ywzgdw: this.form.ywzgdw,
        jzzw: this.form.jzzw,
        jzsj: moment(this.form.jzsj).format('YYYY-MM-DD'),
        jzsl: this.form.jzsl,
        dspToken: this.dspToken,
        images: this.form.images.join(',')
      }).then(({pkValue}) => {
        this.uploadoneattachment(pkValue);
      });
    },
    removeImage(val) {
      this.fileList = this.fileList.filter((item, index) => {
        if (val.content === item.content) {
          this.form.images.splice(index, 1);
        } else {
          return item;
        }
      });
    },
    afterRead(file) {
      // let toast = Toast.loading({
      //     duration: 0,
      //     mask: true,
      //     message: '上传中...'
      //   }), formData = new FormData();
      // formData.append('file', file.file);
      // formData.append('busiId', 'jggb');
      // formData.append('dspToken', this.dspToken);
      // formData.append('busiTable', 'T_LGB_SHJZGL_JZXX');
      // formData.append('busiType', 'jggb_shjzgljzxxtp');
      // let config = {
      //   headers: { 'Content-Type': 'multipart/form-data' }
      // };
      // axios.post('/jggb_web_wx/attachment/uploadattachment.mvc', formData, config).then(({pkValue}) => {
      //   this.form.images.push(pkValue);
      //   toast.clear();
      // });
    },
    async uploadoneattachment(pkValue) {
      for (let item of this.fileList) {
        let formData = new FormData();
        formData.append('file', item.file);
        formData.append('busiId', pkValue || 'jggb');
        formData.append('dspToken', this.dspToken);
        formData.append('busiTable', 'T_LGB_SHJZGL_JZXX');
        formData.append('busiType', 'jggb_shjzgljzxxtp');
        await uploadoneattachment(formData).then(res => {
          console.log(res);
        });
      }
      this.fileList = [];
      this.dialogShow = false;
      this.gethistoryList();
      this.form = {
        shzzmc: '',
        ywzgdw: '',
        jzzw: '',
        jzsj: '',
        jzsl: '',
        images: []
      };
    },
    showDatePicker(type) {
      this.isDatePickerShow = true;
    },
    fetchDate(d) {
      this.form.jzsj = d.originValue;
      this.isDatePickerShow = d.showDatePicker;
    },
    hideDatePicker(d) {
      this.isDatePickerShow = d.showDatePicker;
    }
  }
};
</script>
<style>

</style>
<style lang="less" scoped>
.hospitalsign {
  padding: 24px;
  background: #f5f5f5;
  .top {
    width: 704px;
    background-color: #ffffff;
    box-shadow: 3px 4px 9px 1px rgba(1, 88, 155, 0.05);
    border-radius: 8px;
    margin: 0 auto;
    .title {
      height: 64px;
      background-color: #3198ed;
      color: #ffffff;
      font-size: 30px;
      line-height: 64px;
      padding: 0 16px;
      border-top-left-radius: 8px;
      border-top-right-radius: 8px;
    }
    .topinfo {
      display: flex;
      padding: 35px 18px;
      box-sizing: border-box;
      width: 674px;
      margin: 0 auto;
      font-size: 34px;
      color: #343434;
      &:not(:last-child, .noborder) {
        border-bottom: 1px solid #eeeeee;
      }
      .ploadSies {
        font-size: 25px;
      }
      .post {
        color: #888888;
        text-align: left;
      }
      div:last-child {
        flex: 1;
        text-align: right;
        input {
          text-align: right;
        }
      }
      .special {
        width: 380px;
      }
      .arrow {
        display: flex;
        justify-content: flex-end;
        align-items: center;
        font-size: 34px;
        color: #888;
        text-align: right;
      }
    }
    .noborder {
      padding-top: 0;
      font-size: 20px;
      display: block;
      &>div {
        width: 100%;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
        word-break: break-all;
        display: flex;
        align-items: center;
        justify-content: space-between;
      }
    }
  }
  .sign-btn {
    border: none;
    width: 704px;
    height: 90px;
    background-color: #3198ed;
    box-shadow: 0px 5px 5px 0px rgba(89, 166, 250, 0.1);
    border-radius: 45px;
    line-height: 90px;
    color: #ffffff;
    font-size: 36px;
    text-align: center;
    margin: 26px auto;
    letter-spacing: 7px;
  }
  .historylist {
    .historytitle {
      color: #888888;
      font-size: 30px;
      margin: 19px 0;
    }
    .item {
      width: 704px;
      // height: 140px;
      background-color: #ffffff;
      box-shadow: 3px 4px 9px 1px rgba(1, 88, 155, 0.05);
      border-radius: 8px;
      margin: 0 auto;
      margin-top: 23px;
      position: relative;
      padding: 31px 30px 26px 33px;
      box-sizing: border-box;
      display: flex;
      &::before {
        content: "";
        width: 5px;
        // height: 140px;
        background-color: #3198ed;
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        border-top-left-radius: 50px;
        border-bottom-left-radius: 50px;
      }
      > div:first-child {
        flex: 1;
        p:first-child {
          color: #888888;
          font-size: 30px;
        }
        p:last-child {
          width: calc(100vw - 160px);
          color: #353535;
          font-size: 34px;
          // white-space: nowrap;
          // text-overflow: ellipsis;
          // overflow: hidden;
        }
      }
      > div:last-child {
        width: 50px;
        font-size: 34px;
        color: #b2b2b2;
        align-self: center;
        text-align: right;
      }
    }
  }
  .empty-style{
    color: #bfbfbf;
  }
  /deep/.van-dialog {
    width: 600px;
    background: #fff;
    font-size: 32px;
    .van-dialog__content {
      padding: 15px 30px;
      text-align: left;
      font-size: 28px;
    }
    .van-button--large  {
      height: 80px;
      line-height: 80px;
      font-size: 28px
    }
    .van-dialog__confirm, .van-dialog__confirm:active {
      color: #3198ed
    }
  }
  /deep/ .van-uploader__wrapper {
        .van-uploader__upload {
          width: 156px;
          height: 156px;
          background-color: #bfbfbf;
          .van-icon {
            font-size: 40px;
            color: #fff;
            &:before {
              content: "\F0A2";
            }
          }
        }
        .van-uploader__mask {
          .van-icon {
            font-size: 40px;
            color: #fff;
          }
        }
        .van-uploader__preview-image {
          width: 156px;
          height: 156px;
          .van-icon {
            font-size: 40px;
            color: #fff;
            &:before {
              content: "\F0A2";
            }
          }
        }
        .van-uploader__preview-delete {
          width: 30px;
          height: 30px;
          border-radius: 15px;
          .van-uploader__preview-delete-icon {
            position: absolute;
            font-size: 12px;
            right: 2px;
            top: 2px;
            transform: scale(0.6);
          }
        }
      }
}
.van-button--primary {
  background-color: #ccc;
  border: 1px solid #ccc;
  border-radius: 50px;
}
</style>
